//
//  NewsV1Controller.swift
//  SCA
//
//  Created by tjun on 2025/9/14.
//

import UIKit


class NewsV1Controller: BaseController {
    
    private var datas: [NewsModel] = []

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = AppStyle.color.background
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 150
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(NewsCell.self, forCellReuseIdentifier: NewsCell.identifier)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavigationBarUI()
        
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navBarHeight)
        }

        NewsService.loadNewsData(type: .news) { models in
            self.datas.append(contentsOf: models)
            self.tableView.reloadData()
        }
    }

    private func setNavigationBarUI() {
        
        navigationBar.title = nil
        
        let infoButton = UIButton(image: UIImage(named: "nav_mine_info"), target: self, action: #selector(infoButtonTapped))
        navigationBar.contentView.addSubview(infoButton)
        infoButton.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
        
        let logoImageView = UIImageView(image: UIImage(named: "nav_logo"))
        navigationBar.contentView.addSubview(logoImageView)
        logoImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let logoutButton = UIButton(image: UIImage(named: "nav_logout"), target: self, action: #selector(logoutButtonTapped))
        navigationBar.contentView.addSubview(logoutButton)
        logoutButton.snp.makeConstraints { make in
            make.right.equalTo(-10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
    }
    
    @objc func infoButtonTapped() {
        let mineController = MineController()
        mineController.showInController(self)
    }
    
    @objc func logoutButtonTapped() {
        NotificationCenter.default.post(name: NotificationName.userLogout, object: nil)
    }
}


extension NewsV1Controller: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: NewsCell.identifier, for: indexPath) as! NewsCell
        let model = datas[indexPath.row]
        cell.model = model
        return cell
    }
    
}


